package algocasts;

import java.util.Stack;

public class P122_ImplementQueueUsingStacks {
    class MyQueue {
        private Stack<Integer> in;
        private Stack<Integer> out;

        private void transferIfEmpty() {
            if (out.empty()) {
                while (!in.empty())
                    out.push(in.pop());
            }
        }

        public MyQueue() {
            in = new Stack<>();
            out = new Stack<>();
        }

        public void push(int x) {
            in.push(x);
        }

        public int pop() {
            transferIfEmpty();
            return out.pop();
        }

        public int peek() {
            transferIfEmpty();
            return out.peek();
        }

        public boolean empty() {
            return in.empty() || out.empty();
        }
    }
}
